//
//  RMMOrderTableViewCell.m
//  Romomo
//
//  Created by puman on 2016/10/21.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMOrderTableViewCell.h"

@implementation RMMOrderTableViewCell
{
    UILabel *_orderNum;
    UILabel *_statLabel;
    UILabel *_detailsLabel;
    UIView *_backView;
    UIView *_lineView;
    UILabel *_keFuLabel;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake(15, kRow, kScreenWideth - 30, 190)];
        _backView.backgroundColor = kWhiteColor;
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = 10;
        _backView.layer.borderWidth = 1;
        _backView.layer.borderColor = COLORFROMCODE(0xEEEEEE, 1).CGColor;
        [self.contentView addSubview:_backView];
        
        UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _backView.width, 40)];
        bView.backgroundColor = COLORFROMCODE(0xF3F2F3, 1);
        [_backView addSubview:bView];
        
        _orderNum = [[UILabel alloc] initWithFrame:CGRectMake(kRow, 0, bView.width - 110, bView.height)];
        _orderNum.textColor = COLORFROMCODE(0x9B9B9B, 1);
        _orderNum.font = kFont(13);
        [bView addSubview:_orderNum];
        
        _keFuLabel = [[UILabel alloc] initWithFrame:CGRectMake(_orderNum.right, 10, 60, 20)];
        _keFuLabel.font = kFont(13);
        _keFuLabel.textAlignment = NSTextAlignmentCenter;
        _keFuLabel.backgroundColor = COLORFROMCODE(0xF95B45, 1);
        _keFuLabel.textColor = kWhiteColor;
        _keFuLabel.text = @"客服下单";
        _keFuLabel.hidden = YES;
        [bView addSubview:_keFuLabel];
        
        _statLabel = [[UILabel alloc] initWithFrame:CGRectMake(bView.width - 70, 0, 60, bView.height)];
        _statLabel.textAlignment = NSTextAlignmentRight;
        _statLabel.font = kFont(13);
        _statLabel.textAlignment = NSTextAlignmentRight;
        [bView addSubview:_statLabel];
        
        for (int i = 0; i < 100; i++) {
            RMMOrderCellView *cellView = [[RMMOrderCellView alloc] initWithFrame:CGRectMake(0, 40 + 80 * i, _backView.width, 80)];
            cellView.hidden = YES;
            cellView.tag = 5 + i;
            [_backView addSubview:cellView];
        }
        
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(kRow, 120, _backView.width - kRow, 1)];
        _lineView.backgroundColor = COLORFROMCODE(0xEEEEEE, 1);
        [_backView addSubview:_lineView];
        
        _detailsLabel = [[UILabel alloc] initWithFrame:CGRectMake(kRow, _lineView.bottom, _backView.width - kMaric, 35)];
        _detailsLabel.textColor = COLORFROMCODE(0xBFBFBF, 1);
        _detailsLabel.font = kFont(13);
        _detailsLabel.textAlignment = NSTextAlignmentRight;
        [_backView addSubview:_detailsLabel];

        _leftBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _leftBut.frame = CGRectMake(kRow, _detailsLabel.bottom, 80, 30);
        _leftBut.titleLabel.font = kFont(13);
        _leftBut.layer.masksToBounds = YES;
        _leftBut.layer.cornerRadius = 15;
        _leftBut.layer.borderWidth = 1;
        [_leftBut setTitleColor:kBasicColor forState:(UIControlStateNormal)];
        [_backView addSubview:_leftBut];
        
        _rightBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _rightBut.frame = CGRectMake(_backView.width - 85,_leftBut.top, 70, 30);
        _rightBut.titleLabel.font = kFont(13);
        [_rightBut setTitleColor:kBasicColor forState:(UIControlStateNormal)];
        _rightBut.layer.cornerRadius = 15;
        _rightBut.layer.borderColor = kBasicColor.CGColor;
        _rightBut.layer.borderWidth = 1;
        [_backView addSubview:_rightBut];

    }
    return self;
}

- (void)setModel:(RMMOrderModel *)model{
    _orderNum.text = [NSString stringWithFormat:@"订单编号:%@",[RMMFileManager retrunObjWith:model.ORDER_NO]];
    _statLabel.textColor = kBasicColor;
    NSArray *array = model.BOOK_DETAIL;
    NSInteger totalCount = 0;
    CGFloat totalPrice = 0;
    for (int i = 0; i < 100; i++) {
        RMMOrderCellView *cellView = [self viewWithTag:5 + i];
        if (i < array.count) {
            NSDictionary *dic = array[i];
            cellView.hidden = NO;
            cellView.titleLabel.text = [RMMFileManager retrunObjWith:dic[@"PROD_NAME"]];
            cellView.timeLabel.text = [RMMFileManager retrunObjWith:model.BEGIN_DATE];
            NSString *url = nil;
            url = [RMMFileManager retrunObjWith:dic[@"SER_IMG_URL"]];
            [cellView.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseImageUrl,url.length == 0 ?[RMMFileManager retrunObjWith:dic[@"BOM_IMG_URL"]] : url]] placeholderImage:[UIImage imageNamed:@"加载中@3x"]];
            cellView.priceLabel.text = [RMMFileManager retrunObjWith:[NSString stringWithFormat:@"¥ %@",dic[@"PROD_PRICE"]]];
            CGFloat width = [UILabel backHeightWithString:cellView.priceLabel.text CGFloatCount:30 metho:WIDTH font:16] + 5;
            cellView.qtyLabel.text = [RMMFileManager retrunObjWith:[NSString stringWithFormat:@"x %@",dic[@"PROD_CNT"]]];;
            CGFloat p = [dic[@"PROD_PRICE"] floatValue];
            NSInteger c = [dic[@"PROD_CNT"] integerValue];
            cellView.titleLabel.frame = CGRectMake(cellView.imageV.right + kRow, cellView.imageV.top, cellView.width - width - cellView.imageV.right - kMaric, kMaric);
            totalPrice += p * c;
            totalCount += c;
        } else {
            cellView.hidden = YES;
        }
    }
    _backView.frame = CGRectMake(15, kRow, kScreenWideth - 30, 110 + array.count * 80);
    _lineView.frame = CGRectMake(10, 40 + 80 * array.count, _backView.width - 20, 1);
    _detailsLabel.frame = CGRectMake(kRow, _lineView.bottom, _backView.width - kMaric, 35);
    _leftBut.frame = CGRectMake(kRow, _detailsLabel.bottom, 80, 30);
    _rightBut.frame = CGRectMake(_backView.width - 85,_leftBut.top, 70, 30);
    _detailsLabel.text = [NSString stringWithFormat:@"共%ld件 总计¥ %.2f",totalCount,totalPrice];
    NSString *price = [NSString stringWithFormat:@"%.2f",totalPrice];
    [RMMFileManager changeLabelTextColorOneWithLabel:_detailsLabel forRange:NSMakeRange(_detailsLabel.text.length - price.length - 2, price.length + 2) byColor:[UIColor blackColor]];
    CGFloat w = [UILabel backHeightWithString:_orderNum.text CGFloatCount:44 metho:WIDTH font:13];
    _orderNum.frame =  CGRectMake(kRow, 0, w + 5, 40);
    if ([model.SOURCE isEqualToString:@"3"] || [model.SOURCE isEqualToString:@"0"]) {
        _keFuLabel.hidden = NO;
        if (kScreenWideth == 320) {
            _keFuLabel.frame = CGRectMake(_statLabel.left - 60, 10, 60, 20);
            _orderNum.frame =  CGRectMake(kRow, 0, _backView.width - kRow - _keFuLabel.left, 40);
        } else {
            _keFuLabel.frame = CGRectMake(_orderNum.right, 10, 60, 20);
        }
        model.ORDER_STATUS = [NSString stringWithFormat:@"%@",model.ORDER_STATUS];
        model.BOOK_STATUS = [NSString stringWithFormat:@"%@",model.BOOK_STATUS];
        if ([model.BOOK_STATUS isEqualToString:@"1"] || [model.BOOK_STATUS isEqualToString:@"2"]) {
            _statLabel.text = @"待服务";
            _statLabel.textColor = kBasicColor;
            [self leftButBWith:@"取消订单"];
            [self rigBut];
        } else if ([model.BOOK_STATUS isEqualToString:@"3"]){
            _statLabel.text = @"服务中";
            [self leftButBWith:@""];
            _statLabel.textColor = COLORFROMCODE(0xF39805, 1);
            [self rightButBWith:@"加单"];
        } else if ([model.BOOK_STATUS isEqualToString:@"4"]) {
            //待评价
            _statLabel.text = @"待评价";
            _statLabel.textColor = kBasicColor;
            [self rightButCWith:@"评价获得颜值分"];
            [self leftButBWith:@""];
        } else if([model.BOOK_STATUS isEqualToString:@"5"]){
            // 已完成
            _statLabel.text = @"已完成";
            _statLabel.textColor = kBasicColor;
            [self leftButBWith:@""];
            [self rigBut];
        } else if ([model.BOOK_STATUS isEqualToString:@"9"]){
            _statLabel.text = @"已取消";
            _statLabel.textColor = COLORFROMCODE(0x9B9B9B, 1);
            [self leftButBWith:@""];
            [self rigBut]; 
        }
    } else {
        _keFuLabel.hidden = YES;
        model.ORDER_STATUS = [NSString stringWithFormat:@"%@",model.ORDER_STATUS];
        model.BOOK_STATUS = [NSString stringWithFormat:@"%@",model.BOOK_STATUS];
        if ([model.ORDER_STATUS isEqualToString:@"0"] || [model.ORDER_STATUS isEqualToString:@"1"]) {
            if ([model.BOOK_STATUS isEqualToString:@"0"] || [model.BOOK_STATUS isEqualToString:@"1"] || [model.BOOK_STATUS isEqualToString:@"2"] || [model.BOOK_STATUS isEqualToString:@"3"] || model.BOOK_STATUS == nil) {
                // 进行中
                if ([model.ORDER_STATUS isEqualToString:@"0"]) {
                    _statLabel.text = @"待支付";
                    _statLabel.textColor = COLORFROMCODE(0xF95B45, 1);
                    [self leftButBWith:@"取消订单"];
                    [self rightButAWith:@"去支付"];
                } else {
                    if ([model.BOOK_STATUS isEqualToString:@"1"] || [model.BOOK_STATUS isEqualToString:@"2"]) {
                        _statLabel.text = @"待服务";
                        _statLabel.textColor = kBasicColor;
                        if ([model.IS_UPDATE isEqualToNumber:@0]) {
                            [self leftButBWith:@"申请退款"];
                            [self rightButBWith:@"修改预约"];
                        } else {
                            [self leftButBWith:@"申请退款"];
                            [self rigBut];
                        }
                    } else {
                        _statLabel.text = @"服务中";
                        [self leftButBWith:@""];
                        _statLabel.textColor = COLORFROMCODE(0xF39805, 1);
                        [self rightButBWith:@"加单"];
                    }
                }
                
            }  else {
                if ([model.BOOK_STATUS isEqualToString:@"4"]) {
                    //待评价
                    _statLabel.text = @"待评价";
                    _statLabel.textColor = kBasicColor;
                    [self rightButCWith:@"评价获得颜值分"];
                    [self leftButBWith:@""];
                } else if([model.BOOK_STATUS isEqualToString:@"5"]){
                    // 已完成
                    _statLabel.text = @"已完成";
                    _statLabel.textColor = kBasicColor;
                    [self leftButBWith:@""];
                    [self rigBut];
                } else {
                    _statLabel.text = @"待支付";
                    _statLabel.textColor = COLORFROMCODE(0xF95B45, 1);
                    [self leftButBWith:@"取消订单"];
                    [self rightButAWith:@"去支付"];
                }
            }
        } else if ([model.ORDER_STATUS isEqualToString:@"8"]){
            _statLabel.text = @"退款中";
            _statLabel.textColor = COLORFROMCODE(0xF95B45, 1);
            [self leftButBWith:@"退款详情"];
            [self rigBut];
            
        } else if ([model.ORDER_STATUS isEqualToString:@"10"]) {
            _statLabel.text = @"退款完成";
            _statLabel.textColor = COLORFROMCODE(0x9B9B9B, 1);
            [self leftButBWith:@"退款详情"];
            [self rigBut];
            
        } else if ([model.ORDER_STATUS isEqualToString:@"9"]){
            _statLabel.text = @"已取消";
            _statLabel.textColor = COLORFROMCODE(0x9B9B9B, 1);
            [self leftButBWith:@""];
            [self rigBut];
        }
    }
}
- (void)leftButAWith:(NSString *)title{
    [_leftBut setTitle:title forState:(UIControlStateNormal)];
    _leftBut.frame = CGRectMake(10, _detailsLabel.bottom, 110, 30);
    _leftBut.layer.borderColor = kBasicColor.CGColor;
}
- (void)leftButBWith:(NSString *)title{
    [_leftBut setTitle:title forState:(UIControlStateNormal)];
    _leftBut.frame = CGRectMake(kRow, _detailsLabel.bottom, 80, 30);
    _leftBut.layer.borderColor = kWhiteColor.CGColor;
}
- (void)rightButCWith:(NSString *)title{
    [_rightBut setTitle:title forState:(UIControlStateNormal)];
    _rightBut.backgroundColor = kWhiteColor;
    _rightBut.frame = CGRectMake(_backView.width - 125,_leftBut.top, 110, 30);
    [_rightBut setTitleColor:kBasicColor forState:(UIControlStateNormal)];
    _rightBut.layer.borderColor = kBasicColor.CGColor;

}
- (void)rigBut{
    _rightBut.frame = CGRectMake(_backView.width - 85,_leftBut.top, 70, 30);
    [_rightBut setTitle:@"" forState:(UIControlStateNormal)];
    _rightBut.backgroundColor = kWhiteColor;
    [_rightBut setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
    _rightBut.layer.borderColor = kWhiteColor.CGColor;
}
- (void)rightButAWith:(NSString *)title{
    _rightBut.frame = CGRectMake(_backView.width - 85,_leftBut.top, 70, 30);
    [_rightBut setTitle:title forState:(UIControlStateNormal)];
    _rightBut.backgroundColor = kBasicColor;
    _rightBut.layer.borderColor = kBasicColor.CGColor;
    [_rightBut setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
}
- (void)rightButBWith:(NSString *)title{
    _rightBut.frame = CGRectMake(_backView.width - 85,_leftBut.top, 70, 30);
    [_rightBut setTitle:title forState:(UIControlStateNormal)];
    _rightBut.backgroundColor = kWhiteColor;
    [_rightBut setTitleColor:kBasicColor forState:(UIControlStateNormal)];
    _rightBut.layer.borderColor = kBasicColor.CGColor;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
